<?php

class Portfolio_images extends CI_Model {

    private static $_table_name = 'portofolio_image';
    public $idportofolio_image;
    public $idportofolio;
    public $prioritas = 0;
    public $url;

    function __construct() {
        parent::__construct();
        $this->load->database();
    }

    function build_data() {
        return array(
            'idportofolio_image' => $this->idportofolio_image,
            'idportofolio' => $this->idportofolio,
            'prioritas' => $this->prioritas,
            'url' => $this->url,
        );
    }
    
    function delete($id) {
        $this->db->delete(self::$_table_name, array('idportofolio_image' => $id));
    }

    function get($id = NULL) {
        if($id != NULL) {
            $this->db->where('idportofolio', $id);
        }
        return $this->db->order_by('prioritas DESC')->get(self::$_table_name)->result();
    }
    
    function get_by_id($id) {
        $query = $this->db->get_where(self::$_table_name, array('idportofolio_image' => $id));
        
        if ($query->num_rows() < 1) {
            return null;
        }
        
        $row = $query->row();
        $this->idportofolio_image = $row->idportofolio_image;
        $this->idportofolio = $row->idportofolio;
        $this->prioritas = $row->prioritas;
        $this->url = $row->url;

        return $this;
    }

    function insert() {
        return $this->db->insert(self::$_table_name, $this->build_data());
    }
    
    function update() {
        $this->db->where('idportofolio_image', $this->idportofolio_image);
        $this->db->update(self::$_table_name, $this->build_data());
    }

}